package product

import (
	"API/client"
	"API/request"
	"API/response"
	"common/utils"
	"github.com/gin-gonic/gin"
	product "product_server/proto"
)

// SnappedSpikesProduct 秒杀结算
func SnappedSpikesProduct(c *gin.Context) {
	var req request.SnappedSpikesProductRequest
	if err := c.ShouldBind(&req); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}
	userId := c.GetUint("userId")
	res, err := client.SnappedSpikesProduct(c, &product.SnappedSpikesProductRequest{
		UserId:          int64(userId),
		SpikesProductId: req.ProductId,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "秒杀结算成功", map[string]interface{}{"car": res})
}

// SpikesProductList 秒杀商品列表展示
func SpikesProductList(c *gin.Context) {
	var req request.SpikesProductListRequest
	if err := c.ShouldBind(&req); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}

	res, err := client.SpikesProductList(c, &product.SpikesProductListRequest{
		Pages:       req.Pages,
		PagePerNums: req.PagePerNums,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "秒杀商品列表展示", map[string]interface{}{"SpikesProductList": res})
}

// SpikesProductInfo 秒杀商品详情展示
func SpikesProductInfo(c *gin.Context) {
	var req request.ProductInfoRequest
	// 修改：使用 ShouldBindQuery 处理 GET 请求的查询参数
	if err := c.ShouldBindQuery(&req); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}

	res, err := client.SpikesProductInfo(c, &product.SpikesProductInfoRequest{
		Id: req.Id,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}
	response.CurrencySuccessResponse(c, "秒杀商品详情展示", map[string]interface{}{"SpikesProductInfo": res})
}
